﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Mvc.Ajax;
using System.Web.Routing;

namespace FoundationLib.Web.Mvc
{
	public static class ActionLinkHelper
	{
		public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText, 
			string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
		{
			TagBuilder builder = new TagBuilder("img");
			builder.MergeAttribute("src", UrlHelper.GenerateContentUrl(imageUrl, helper.ViewContext.HttpContext));
			builder.MergeAttribute("alt", altText);
			builder.MergeAttribute("title", altText);
			if (htmlAttributes != null)
				builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

			string link = helper.ActionLink("{replace}", actionName, controllerName, routeValues, ajaxOptions).ToHtmlString()
				.Replace("{replace}", builder.ToString(TagRenderMode.SelfClosing));

			return MvcHtmlString.Create(link);
		}

		public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,
			string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions)
		{
			return (ImageActionLink(helper, imageUrl, altText, actionName, controllerName, routeValues, ajaxOptions, null));
		}

		public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,
			string actionName, string controllerName, AjaxOptions ajaxOptions)
		{
			return (ImageActionLink(helper, imageUrl, altText, actionName, controllerName, null, ajaxOptions, null));
		}

		public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,
			string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
		{
			TagBuilder builder = new TagBuilder("img");
			builder.MergeAttribute("src", UrlHelper.GenerateContentUrl(imageUrl, helper.ViewContext.HttpContext));
			builder.MergeAttribute("alt", altText);
			builder.MergeAttribute("title", altText);
			if (htmlAttributes != null)
				builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

			string link = helper.ActionLink("{replace}", actionName, controllerName, routeValues, ajaxOptions).ToHtmlString()
				.Replace("{replace}", builder.ToString(TagRenderMode.SelfClosing));

			return MvcHtmlString.Create(link);
		}

		public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,
			string actionName, string controllerName, AjaxOptions ajaxOptions, object htmlAttributes)
		{
			return (ImageActionLink(helper, imageUrl, altText, actionName, controllerName, null, ajaxOptions, htmlAttributes));
		}

		public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,
			string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
		{
			return (ImageActionLink(helper, imageUrl, altText, actionName, null, routeValues, ajaxOptions, htmlAttributes));
		}

		public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,
			string actionName, object routeValues, AjaxOptions ajaxOptions)
		{
			return (ImageActionLink(helper, imageUrl, altText, actionName, null, routeValues, ajaxOptions, null));
		}

		public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,
			string actionName, AjaxOptions ajaxOptions)
		{
			return (ImageActionLink(helper, imageUrl, altText, actionName, null, null, ajaxOptions, null));
		}

		public static MvcHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,
			string actionName, AjaxOptions ajaxOptions, object htmlAttributes)
		{
			return (ImageActionLink(helper, imageUrl, altText, actionName, null, null, ajaxOptions, htmlAttributes));
		}

		public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imageUrl, string altText,
			string actionName, string controllerName, object routeValues, object htmlAttributes)
		{
			TagBuilder builder = new TagBuilder("img");
			builder.MergeAttribute("src", UrlHelper.GenerateContentUrl(imageUrl, helper.ViewContext.HttpContext));
			builder.MergeAttribute("alt", altText);
			builder.MergeAttribute("title", altText);
			if (htmlAttributes != null)
				builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

			string link = helper.ActionLink("{replace}", actionName, controllerName, routeValues, htmlAttributes).ToHtmlString()
				.Replace("{replace}", builder.ToString(TagRenderMode.SelfClosing));

			return MvcHtmlString.Create(link);
		}

		public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imageUrl, string altText,
			string actionName, object routeValues, object htmlAttributes)
		{
			return ImageActionLink(helper, imageUrl, altText, actionName, null, routeValues, htmlAttributes);
		}

		public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imageUrl, string altText,
			string actionName)
		{
			return ImageActionLink(helper, imageUrl, altText, actionName, null, null, null);
		}

		public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imageUrl, string altText,
			string actionName, string controllerName)
		{
			return ImageActionLink(helper, imageUrl, altText, actionName, controllerName, null, null);
		}

		public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imageUrl, string altText,
			string actionName, object routeValues)
		{
			return ImageActionLink(helper, imageUrl, altText, actionName, null, routeValues, null);
		}
	}
}
